﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC.AuxiliaryCurve
{
	public class PlaneDisPointsGet : PlaneGetPoint
	{
		public PlaneDisPointsGet(ExternalCommandData cmdArgu, XYZ pointPickArgu, Element elementArgu, double xDisArgu, double yDisArgu, double xSideArgu, double ySideArgu, ref Dictionary<XYZ, XYZ> dicxArgu, ref Dictionary<XYZ, XYZ> dicYArgu) : base(cmdArgu, pointPickArgu, elementArgu, xSideArgu, ySideArgu, ref dicxArgu, ref dicYArgu)
		{
			this.xDis = xDisArgu;
			this.yDis = yDisArgu;
		}

		public override bool GetPoints()
		{
			Face faceofPoint = BoostFunction.GetFaceofPoint(this.m_cmd, this.pointPick, this.element);
			if (faceofPoint == null)
			{
				return false;
			}
			BoundingBoxUV boundingBox = faceofPoint.GetBoundingBox();
			double num = boundingBox.Min.DistanceTo(boundingBox.Max);
			if (num <= this.xSide || num < this.ySide || num <= this.m_cmd.Application.Application.ShortCurveTolerance)
			{
				return false;
			}
			Dictionary<UV, UV> dicArgu = new Dictionary<UV, UV>();
			Dictionary<UV, UV> dicArgu2 = new Dictionary<UV, UV>();
			if (!this.GetUVList(ref dicArgu, ref dicArgu2, boundingBox.Min, boundingBox.Max))
			{
				return false;
			}
			BoostFunction.ConvertUVToXYZ(dicArgu, faceofPoint, ref this.dicX);
			BoostFunction.ConvertUVToXYZ(dicArgu2, faceofPoint, ref this.dicY);
			return true;
		}

		private bool GetUVList(ref Dictionary<UV, UV> dicXArgu, ref Dictionary<UV, UV> dicYArgu, UV min, UV max)
		{
			if (dicXArgu == null || dicYArgu == null)
			{
				return false;
			}
			double num = max.V - this.ySide;
			do
			{
				double num2 = num;
				double num3 = min.U + this.xSide;
				double num4 = num;
				double u = max.U;
				UV key = new UV(num3, num2);
				UV value = new UV(u, num4);
				dicXArgu.Add(key, value);
				num -= this.xDis;
			}
			while (num > min.V);
			double num5 = min.U + this.xSide;
			do
			{
				double num6 = num5;
				double v = min.V;
				double num7 = num5;
				double num8 = max.V - this.ySide;
				UV key2 = new UV(num6, v);
				UV value2 = new UV(num7, num8);
				dicYArgu.Add(key2, value2);
				num5 += this.yDis;
			}
			while (num5 < max.U);
			return true;
		}

		protected double xDis;

		protected double yDis;
	}
}
